<script setup lang="ts">
import { computed, ref } from 'vue'
import {
    Iphone,
    Location,
    OfficeBuilding,
    Tickets,
    User,
    Lock,
    Message,
    ChatRound,
    Position,
    Calendar,
    CreditCard,
    StarFilled

} from '@element-plus/icons-vue'
import { reactive } from 'vue'
import { ElMessage, FormRules } from 'element-plus';

const size = ref('Large')
const iconStyle = computed(() => {
    const marginMap = {
        large: '8px',
        default: '6px',
        small: '4px',
    }
    return {
        marginRight: marginMap[size.value] || marginMap.default,
    }
})
const blockMargin = computed(() => {
    const marginMap = {
        large: '32px',
        default: '28px',
        small: '24px',
    }
    return {
        marginTop: marginMap[size.value] || marginMap.default,
    }
})

interface RuleForm {
    username: String,
    id: Number,
    nickname: String,
    email: String,
    ruler: Number,
    user_pic: String,
    phone: String,
    sex: String,
    age: Number,
    hobby: String

}

const addData = ref<RuleForm>({
    username: '',
    id: null,
    nickname: '',
    email: '',
    ruler: null,
    user_pic: '',
    phone: '',
    sex: '',
    age: null,
    hobby: ''
    
})

import { getUserinfoServer } from '@/api/user.js'
import useUserInfo from '@/stores/userinfo.js'
import moment from 'moment';
const userinfo = getUserinfoServer();
const useuserinfo = useUserInfo() as any;
const getUserinfo = async()=>{
    let result = await getUserinfoServer();
    useuserinfo.setInfo(result.data);
    useuserinfo.info.updateTime = moment(useuserinfo.info.updateTime).format('YYYY-MM-DD HH:mm')
    useuserinfo.info.createTime = moment(useuserinfo.info.createTime).format('YYYY-MM-DD HH:mm')
}
getUserinfo();




const dialogVisible = ref(false)
const dialogname = ref()
import { updatauserServer } from '@/api/user.js';
const updatauser = async () => {
    dialogVisible.value = false;
    let result = await updatauserServer(addData.value) as any;
    ElMessage.success(result.message ? result.message : '修改成功')
    getUserinfo()
}

const reg_email = /(^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$)/
const reg_phone = /(^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$)/
const reg_age=/^(?:[1-9][0-9]?|1[01][0-9]|120)$/;//年龄是1-120之间有效

const rules = reactive<FormRules<RuleForm>>({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 15, message: '用户名要求3到15位的非空字符串', trigger: 'blur' }
    ],

    email: [
        { required: false, message: '请输入邮箱', trigger: 'blur' },
        { pattern: reg_email, message: '名称允许汉字、字母、数字，域名只允许英文域名', trigger: 'blur' }
    ],

    phone: [
        { required: false, message: '请输入手机号', trigger: 'blur' },
        { pattern: reg_phone, message: '请输入正确的手机号', trigger: 'blur' }
    ],

    age: [
        { required:false,message:'请输入年龄',trigger:'blur'},
        { pattern: reg_age,message:'年龄要求0到120岁',trigger:'blur'}
    ],


    nickname: [
        { required: false, message: '请输入昵称', trigger: 'blur' },
        { min: 1, max: 15, message: '昵称要求1到15位', trigger: 'blur' }
    ]
})

const clearadddata = () => {
    addData.value = {
        username: '',
        id: null,
        nickname: '',
        email: '',
        ruler: null,
        phone: '',
        user_pic: '',
        sex: '',
        age: null,
        hobby: ''
    }
}

const showupdataDiglog = () => {
    dialogVisible.value = true
    dialogname.value = '修改用户信息'
    addData.value.username = useuserinfo.info.username
    addData.value.nickname = useuserinfo.info.nickname
    addData.value.email = useuserinfo.info.email
    addData.value.id = useuserinfo.info.id
    addData.value.phone = useuserinfo.info.phone
    addData.value.ruler = useuserinfo.info.ruler
    addData.value.age = useuserinfo.info.age
    addData.value.sex = useuserinfo.info.sex
    addData.value.hobby = useuserinfo.info.hobby
    

}
</script>

<template>
    <el-card style="max-width: 100%">
        <div class="card-header">
            <span>个人信息</span>
        </div>
        <div style="margin-top: 20px;">
            <hr>
        </div>

        <el-descriptions class="margin-top"  :column="1" :size="size" border >
            <template #extra>
                <el-button type="primary" @click="showupdataDiglog()">修改</el-button>
            </template>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <user />
                        </el-icon>
                        用户名
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.username }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <CreditCard />
                        </el-icon>
                        性别
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.sex }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <CreditCard />
                        </el-icon>
                        年龄
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.age }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <StarFilled />
                        </el-icon>
                        兴趣爱好
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.hobby }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <iphone />
                        </el-icon>
                        手机号
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.phone }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <ChatRound />
                        </el-icon>
                        昵称
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.nickname }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <Message />
                        </el-icon>
                        邮箱
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.email }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <office-building />
                        </el-icon>
                        角色
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.ruler }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <Calendar />
                        </el-icon>
                        创建时间
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.createTime }}</el-text>
            </el-descriptions-item>
            <el-descriptions-item>
                <template #label>
                    <div class="cell-item">
                        <el-icon :style="iconStyle">
                            <Calendar />
                        </el-icon>
                        最后修改时间
                    </div>
                </template>
                <el-text type="primary">{{ useuserinfo.info.updateTime }}</el-text>
            </el-descriptions-item>
        </el-descriptions>

        <el-dialog v-model="dialogVisible" :title="dialogname" width="500">
            <el-form ref="form" class="add" :rules="rules" :model="addData">
                <el-form-item prop="nickname">
                    <el-input :prefix-icon="ChatRound" type="nickname" 
                        v-model="addData.nickname"></el-input>
                </el-form-item>
                <el-form-item prop="age">
                    <el-input :prefix-icon="CreditCard" type="age" 
                        v-model="addData.age"></el-input>
                </el-form-item>
                <el-form-item prop="hobby">
                    <el-input :prefix-icon="StarFilled" type="hobby" 
                        v-model="addData.hobby"></el-input>
                </el-form-item>
                <el-form-item prop="email">
                    <el-input :prefix-icon="Message" type="email" 
                        v-model="addData.email"></el-input>
                </el-form-item>
                <el-form-item prop="phone">
                    <el-input :prefix-icon="Position" type="phone" 
                        v-model="addData.phone"></el-input>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="addData.sex" placeholder="请选择" clearable>
                        <el-option label="男" value="男" />
                        <el-option label="女" value="女" />
                        <el-option label="中性" value="中性" />
                    </el-select>
                </el-form-item>
                <el-form-item >
                    <el-select v-model="addData.ruler" placeholder="请选择" clearable>
                        <el-option label="普通用户" value="普通用户" />
                        <el-option label="作者" value="作者" />
                        <el-option label="管理员" value="管理员" />
                    </el-select>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary"
                        @click="updatauser()">确认</el-button>
                </div>
            </template>
        </el-dialog>

    </el-card>
</template>


<style scoped>
.el-descriptions {
    margin-top: 20px;
}

.cell-item {
    display: flex;
    align-items: center;
}

.margin-top {
    margin-top: 20px;
}
</style>